package com.emokili.ewa.core.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emokili.ewa.common.exception.BizCodeEnum;
import com.emokili.ewa.common.exception.BusinessException;
import com.emokili.ewa.core.frame.dao.MemberDao;
import com.emokili.ewa.core.frame.dao.MemberFriendDao;
import com.emokili.ewa.core.frame.entity.MemberEntity;
import com.emokili.ewa.core.frame.entity.MemberFriendEntity;
import com.emokili.ewa.core.frame.service.MemberFriendService;
import com.emokili.ewa.core.frame.service.MemberService;
import com.emokili.ewa.core.frame.vo.MemberFriendVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MemberFriendServiceImpl extends ServiceImpl<MemberFriendDao, MemberFriendEntity> implements MemberFriendService {

    @Autowired
    private MemberFriendDao memberFriendDao;
    @Autowired
    private MemberService memberService;

    @Override
    public void inviteFriend(MemberFriendVo memberFriendVo) {
       String friendAccount = memberFriendVo.getFriendAccount();
        //1、去数据库查询 SELECT * FROM ums_member WHERE username = ? OR email = ?
        MemberEntity memberEntity = memberService.getOne(new QueryWrapper<MemberEntity>()
                .eq("user_name", friendAccount).or().eq("email",friendAccount));
        if(memberEntity == null){
            throw new BusinessException(BizCodeEnum.USER_NAME_OR_EMAIL_NOT_FOUND);
        }
        MemberFriendEntity memberFriendEntity = new MemberFriendEntity();
        BeanUtils.copyProperties(memberFriendVo,memberFriendEntity);
        memberFriendEntity.setMemberFriendId(memberEntity.getId());
        memberFriendEntity.setEmail(memberEntity.getEmail());
        memberFriendEntity.setUserName(memberEntity.getUserName());
        memberFriendEntity.setCreateTime(new Date());
        memberFriendDao.insert(memberFriendEntity);
    }
}
